package com.xhh.adjust.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.adjust.mapper.FaultyMapper;
import com.xhh.adjust.mapper.MajorMapper;
import com.xhh.adjust.service.FaultyService;
import com.xhh.adjust.service.MajorService;
import com.xhh.apis.user.IUserClient;
import com.xhh.common.constants.CheckedConstants;
import com.xhh.model.adjust.dtos.GetMajorDto;
import com.xhh.model.adjust.pojos.Faulty;
import com.xhh.model.adjust.pojos.Major;
import com.xhh.model.common.dtos.ResponseResult;
import com.xhh.model.common.enums.AppHttpCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    /**
     * 根据专业编码获取专业信息
     * @param majorCode
     * @return
     */
    @Override
    public ResponseResult getMajorByCode(String majorCode) {
        // 1. 校验参数
        if (StrUtil.isEmpty(majorCode)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 发起查询
        Major major = lambdaQuery()
                .eq(Major::getMajorCode, majorCode)
                .one();
        if (major == null || major.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(major);
    }

    /**
     * 根据单位id和院系id获取专业列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getMajorByUidAndFid(GetMajorDto dto) {
        // 1. 校验参数
        if (dto == null || dto.getUnityId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 构造查询条件，默认根据招生单位id查询专业列表
        List<Major> list = lambdaQuery()
                .eq(Major::getUnityId, dto.getUnityId())
                .eq(dto.getFaultyId() != null, Major::getFaultyId, dto.getFaultyId())
                .list();
        if (CollUtil.isEmpty(list)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3. 返回结果
        return ResponseResult.okResult(list);
    }
}
